package com.ey12.db.dao;

import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ey12.db.domain.GmGame;
import com.ey12.db.domain.GmOrder;
import com.ey12.utils.DateUtil;

/**
 * Data access object (DAO) for domain model class GmGame.
 * 
 * @see com.ey12.db.domain.GmGame
 * @author MyEclipse Persistence Tools
 */

public class GmGameDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(GmGameDAO.class);
	// property constants
	public static final String GAME_NAME = "gameName";
	public static final String ORDER_NUM = "orderNum";
	public static final String INDEXSEQ = "indexSeq";
	public static final String IS_HOT = "isHot";

	protected void initDao() {
		// do nothing
	}

	public void saveOrUpdate(GmGame transientInstance) {
		log.debug("saving GmGame instance");
		try {
			getHibernateTemplate().saveOrUpdate(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(GmGame persistentInstance) {
		log.debug("deleting GmGame instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public GmGame findById(java.lang.Integer id) {
		log.debug("getting GmGame instance with id: " + id);
		try {
			GmGame instance = (GmGame) getHibernateTemplate().get(
					"com.ey12.db.domain.GmGame", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(GmGame instance) {
		log.debug("finding GmGame instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public List findIdByGameName(String gameName) {
			try {
				String queryString = "select distinct id from GmGame where gameName=? ";
				return getHibernateTemplate().find(queryString,gameName);
			} catch (RuntimeException re) {
				log.error("find all failed", re);
				throw re;
			}
		}

//	public List findGameDetialData(String gameName,String cserverName,String serverType,String camp) {
//		try {
//			String queryString = "from GmGame g where 1=1";
//			if(gameName!=null){		
//					queryString=queryString+" and g.gameName='"+gameName+"'";									
//			}
//			if(cserverName!=null){
//					queryString=queryString+" and g.gmCservers.cserverName='"+cserverName+"'";								
//			}
//			if(serverType!=null){
//					queryString=queryString+" and g.gmCservers.severType='"+serverType+"'";								
//			}
//			if(camp!=null){
//				queryString=queryString+" and g.gmCservers.camp='"+camp+"'";								
//			}
//			System.out.println("findFundDetial--queryString="+queryString);
//			return getHibernateTemplate().find(queryString);
//		} catch (RuntimeException re) {
//			re.printStackTrace();
//			log.error("find all failed", re);
//			throw re;
//		}
//	}
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GmGame instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GmGame as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			re.printStackTrace();
			throw re;
		}
	}
	public List findByIndexSeq(Object indexSeq) {
		return findByProperty(INDEXSEQ, indexSeq);
	}

	public List findByGameName(Object gameName) {
		return findByProperty(GAME_NAME, gameName);
	}

	public List findByOrderNum(Object orderNum) {
		return findByProperty(ORDER_NUM, orderNum);
	}

	public List findByIsHot(Object isHot) {
		return findByProperty(IS_HOT, isHot);
	}

	public List findAll() {
		log.debug("finding all GmGame instances");
		try {
			String queryString = "from GmGame";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findHotGameList() {
		try {
			String queryString = "from GmGame where isHot=2 order by orderNum";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List findFirstCbx() {
		try {
			String queryString = "select distinct gameName from GmGame order by gameName";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public Integer getLeastMount(String gameName) {
		try {
			String queryString = "select leastMount from GmGame where gameName='"+gameName+"'";
			List list = getHibernateTemplate().find(queryString);
			if(list.size()>0){
				return Integer.parseInt(list.get(0).toString());
			}else{
				return 0;
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	public List findbvwData() {
		try {
			String queryString = "from GmGame order by gameName";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			re.printStackTrace();
			log.error("find all failed", re);
			throw re;
		}
	}

	public GmGame merge(GmGame detachedInstance) {
		log.debug("merging GmGame instance");
		try {
			GmGame result = (GmGame) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(GmGame instance) {
		log.debug("attaching dirty GmGame instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(GmGame instance) {
		log.debug("attaching clean GmGame instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static GmGameDAO getFromApplicationContext(ApplicationContext ctx) {
		return (GmGameDAO) ctx.getBean("GmGameDAO");
	}
}